﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectSearchActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {

            var webr = UMC.Web.WebResource.Instance();
            var Id = this.AsyncDialog("Id", r =>
            {
                var search = new Subject();
                var form = request.SendValues ?? new UMC.Web.WebMeta();

                int limit = UMC.Data.Utility.IntParse(form["limit"] as string, 25);
                int start = UMC.Data.Utility.IntParse(form["start"] as string, 0);

                string sort = form[("sort")] as string;
                string dir = form[("dir")] as string;

                var category = form["Category"] as string;
                // var pics = new List<UMC.Data.Entities.Picture>();

                Guid? CategoryId = UMC.Data.Utility.Guid(category);

                var Keyword = (form["Keyword"] as string ?? String.Empty);
                switch (Keyword)
                {
                    case "发布":
                    case "等候发布":
                    case "发布中":
                        Keyword = String.Empty;
                        search.Status = 0;
                        break;
                    case "已发布":
                        search.Status = 1;
                        Keyword = String.Empty;
                        break;
                    case "不发布":
                        search.Status = -1;
                        Keyword = String.Empty;
                        break;
                }
                if (String.IsNullOrEmpty(Keyword) == false)
                {
                    search.Title = Keyword;
                }
                if (request.IsCashier == false)
                {
                    search.Status = 1;
                }
                int nextIndex;
                var subs = DataFactory.Instance().Search(search, start, out nextIndex, limit);

                var data = new System.Data.DataTable();
                data.Columns.Add("id");
                data.Columns.Add("title");
                data.Columns.Add("desc");
                data.Columns.Add("time");
                data.Columns.Add("reply");
                data.Columns.Add("look");
                data.Columns.Add("favs");
                data.Columns.Add("tipoffs");
                data.Columns.Add("last");
                data.Columns.Add("status");
                data.Columns.Add("poster");
                data.Columns.Add("images", typeof(System.Data.DataTable));

                data.Columns.Add("type");
                //var count = 108;
                foreach (var sub in subs)
                {
                    if (sub.Status == -2 && request.IsCashier == false)
                    {
                        continue;
                    }

                    var imgs = new System.Data.DataTable();
                    imgs.Columns.Add("src");
                    int indexCount = 0;
                    imgs.Rows.Add(webr.ImageResolve(request.Url, sub.Id.Value, 1, "0") + "!one");

                    var s = "发布中";
                    switch (sub.Status ?? 0)
                    {
                        case -2:
                            s = "驳回";
                            break;
                        case -1:
                            s = "不发布";
                            break;
                        case 0:
                            s = "审阅中";
                            break;
                        case 1:
                            s = "已发布";
                            break;
                    }
                    data.Rows.Add(sub.Id, sub.Title, sub.Description, sub.ReleaseTime.HasValue ? UMC.Data.Utility.GetDate(sub.ReleaseTime) : "未发布"
                        , sub.Reply ?? 0, sub.Look ?? 0, sub.Favs ?? 0, sub.TipOffs ?? 0, UMC.Data.Utility.GetDate(sub.LastDate), s, sub.Poster
                        , imgs, indexCount > 2 ? "CMSThree" : ((sub.IsPicture ?? false) ? "CMSMax" : "CMSOne"));
                }
                var hashc = new System.Collections.Hashtable();
                hashc["data"] = data;
                if (data.Rows.Count == 0 && start == 0)
                {
                    hashc["msg"] = "现在还未有发布的内容";
                }
                else
                {
                    hashc["start"] = nextIndex;// subEntity.Count();

                }
                response.Redirect(hashc);
                return this.DialogValue("auto");
            });
            var type = this.AsyncDialog("Type", "Info");
            var sid = UMC.Data.Utility.UInt64Parse(Id) ?? 0;

            var sub3 = DataFactory.Instance().Subject(sid) ?? new Subject { Id = sid };

            var con = DataFactory.Instance().Content(sub3.Id.Value) ?? new SubjectContent();

            var hash = new WebMeta();
            hash["Content"] = con.Content;
            hash["Code"] = sub3.Code;
            hash.Put("Id", sub3.Id)
            .Put("Title", sub3.Title)
            .Put("project_id", sub3.project_id)
            .Put("project_item_id", sub3.project_item_id)
            .Put("portfolio_id", sub3.portfolio_id);
            if (String.IsNullOrEmpty(con.DataJSON) == false)
            {
                hash["DataJSON"] = con.DataJSON.Replace("\"_CellName\":", "\"type\":");
            }
            hash["ContentType"] = con.ContentType;

            if (String.IsNullOrEmpty(sub3.Code) == false && sub3.project_id.HasValue && sub3.project_item_id.HasValue)
            {

                var project = DataFactory.Instance().Project(sub3.project_id.Value);

                var projectItem = DataFactory.Instance().ProjectItem(sub3.project_item_id.Value);

                if (project != null && projectItem != null)
                {
                    if (type == "Path")
                    {
                        this.Context.Send("Subject.Path", new WebMeta().Put("Path", String.Format("{0}/{1}/{2}", project.Code, projectItem.Code, sub3.Code)), true);
                    }
                    hash["Path"] = String.Format("{0}/{1}/{2}", project.Code, projectItem.Code, sub3.Code);

                }
            }


            response.Redirect(hash);



        }

    }
}